package com.jichaoyun.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jichaoyun.model.vo.ScheduelObject;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface ScheduelObjectMapper extends BaseMapper<ScheduelObject> {

  @Select("WITH LatestMessages AS (select send_id,content,create_time,is_read,ROW_NUMBER() OVER "
      + "(PARTITION BY send_id ORDER BY create_time DESC) AS RowNum from scheduel_realtime ) "
      + "select lm.content lastScheduelContent,lm.create_time lastScheduelTime,is_read,u.username,u.id id from jcy_user u "
      + "LEFT JOIN LatestMessages lm on (u.id = lm.send_id and lm.RowNum = 1) where CHARINDEX((${searchText}),u.username)>0 and u.id != ${id} and u.deleted=0 ORDER BY lastScheduelTime DESC")
  List<ScheduelObject> getScheduelObjectList(@Param("searchText") String searchText,@Param("id")Integer id);

  @Select("WITH LatestMessages AS (select send_id,content,create_time,is_read,ROW_NUMBER() OVER "
      + "(PARTITION BY send_id ORDER BY create_time DESC) AS RowNum from scheduel_realtime ) "
      + "select lm.content lastScheduelContent,lm.create_time lastScheduelTime,u.username,is_read,u.id id from jcy_user u "
      + "LEFT JOIN LatestMessages lm on (u.id = lm.send_id and lm.RowNum = 1) where u.id != ${id} and u.deleted=0 ORDER BY lastScheduelTime DESC")
  List<ScheduelObject> getScheduelObjectListNonSearchText(@Param("id")Integer id);

  @Select("WITH LatestMessages AS (select send_id,receive_id,content,create_time,is_read,ROW_NUMBER() OVER (PARTITION BY send_id ORDER BY create_time DESC) AS RowNum from scheduel_realtime )"
      + "select lm.content lastScheduelContent ,lm.create_time lastScheduelTime, is_read, j.username, j.id id from jcy_user as j RIGHT JOIN LatestMessages as lm on (j.id = lm.send_id and lm.receive_id = ${id} and lm.RowNum = 1) WHERE j.user_role=2" +
          "and j.deleted=0")
  List<ScheduelObject> getScheduelObjectSystem(@Param("id")Integer id);
}
